'''Detects bans to be set on a regexp basis.'''
############################################################################
#    Copyright (C) 2006 by Robert Small                                    #
#    SmallR2002@Gmail.com                                                  #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

__module_name__        = "bcregexpban"
__module_version__     = "0.0.1"
__module_description__ = "Detects regexp bans on join."

import re
import xchat
import bcsettings

def on_join_regexp_ban(word, word_eol, userdata): #IGNORE:W0613
    '''
    Checks against regexp and bans if it's a match.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object:
    '''
    for w in bcsettings.regexp_ban_list:
        if re.match(w, word[0]):
            xchat.command("ban *!*@" + word[2].split("@")[1])
            xchat.command("kick " + word[0] + " RegExp ban (BottleCap)")
            xchat.command("timer 600 mode " + word[1] + " -b *!*@" + word[2].split("@")[1])
            break
            #xchat.command("msg " + word[1] + " RegExp Ban detected, " + word[0] + " matches " + w)
                
        #Next
#End Function

def regexp_ban_check(word, word_eol, userdata): #IGNORE:W0613
    for l in xchat.get_list('users'):
        for w in bcsettings.regexp_ban_list:
            if re.match(w, l.nick):
                xchat.command("ban *!*@" + l.host.split("@")[1])
                xchat.command("kick " + l.nick + " RegExp ban (BottleCap)")
                xchat.command("timer 600 mode " + l.host + " -b *!*@" + l.host.split("@")[1])
                break
    
    return xchat.EAT_ALL

def add_regexp(word, word_eol, userdata): #IGNORE:W0613
    bcsettings.regexp_ban_list.append(word_eol[1])
    return xchat.EAT_ALL